 //
//  WYMyFollowListController.m
//  Boss
//
//  Created by admin on 2018/7/2.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYMyFollowListController.h"
#import "WYFollowCell.h"
#import "WYUserMessage.h"
#import "WYPersonWebHomePageController.h"
#import "WYPersonWebHomePageController.h"
@interface WYMyFollowListController ()<UITableViewDelegate,UITableViewDataSource,WYFollowCellDelegate>
{
    BOOL _isRefresh;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end
@implementation WYMyFollowListController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    
    
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
    if ([self.type isEqualToString:@"1"]) {
        [self setNoDataViweWithTitle:@"您还没有关注的好友哦～" img:[UIImage imageNamed:@"粉丝"]];
    }else{
        [self setNoDataViweWithTitle:@"还没有粉丝关注您哦～" img:[UIImage imageNamed:@"粉丝"]];
    }
    
    
    
}
-(void)refreshloadData
{
    _isRefresh = YES;
    [self loadData];
}
-(void)loadData
{
    WEAKSELF
    [WYHttpServer getAttentionWithType:self.type SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            if (_isRefresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSArray *array =[WYUserMessage getUserMessageArrayFromJson:response[@"result"]];
            if (array.count) {
                 [weakSelf.dataArray addObject:array];
            }
            if (weakSelf.dataArray.count) {
                [weakSelf hiddenNoDataView];
            }else{
                [weakSelf showNoDataView];
            }
            
            
            
           
            [weakSelf.tableView reloadData];
        }
        
        
        
    }];
}


-(void)setupUI
{
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYFollowCell" bundle:nil] forCellReuseIdentifier:@"WYFollowCell"];
    self.tableView.rowHeight = 60;
    if (@available(iOS 11.0, *)){
        [self.tableView setContentInsetAdjustmentBehavior:(UIScrollViewContentInsetAdjustmentNever)];
    }
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYFollowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYFollowCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYFollowCell" owner:nil options:nil].firstObject;
    }
    cell.delegate = self;
    WYUserMessage *userMessage =self.dataArray[indexPath.section][indexPath.row];
    [cell setUserMessage:userMessage WithType:self.type];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYUserMessage *userMessage =self.dataArray[indexPath.section][indexPath.row];
    WYPersonWebHomePageController *web = [[WYPersonWebHomePageController alloc]init];
    
    web.urlString = userMessage.id;
    [self.navigationController pushViewController:web animated:YES];
    
    
    
    
}




-(void)clickStateAction:(WYFollowCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    WYUserMessage *userMessage =self.dataArray[indexPath.section][indexPath.row];
    BOOL state =userMessage.isLove.integerValue;
    state = !state;
    if ([self.type isEqualToString:@"1"]) {
        state = NO;
    }
    WEAKSELF
   [WYHttpServer followUserOrUnFollowUser:userMessage.id WithAttentionState:[NSString stringWithFormat:@"%d",state] SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       if (success) {
           _isRefresh = YES;
           [weakSelf loadData];
       }else{
           
       }
       
       
       
    }];
    
    
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}




- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
